<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Post extends Admin
{
    protected $table_id = false;
    protected $with = ['user_index','post_class','post_image'];
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
                ['type' => 'text', 'name' => '发布时间', 'key' => 'create_time','disabled'=>1],
                ['type' => 'text', 'name' => '类型', 'key' => 'type_b','disabled'=>1],
                ['type' => 'text', 'name' => '所属城市', 'key' => 'city','disabled'=>1],
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    //列表-页面
    public function index()
    {
        $PostClass_model = app('app\admin\model\PostClass');
        $data = $PostClass_model->where('type',1)->select();
        if(!empty($data) && !empty($data->toArray()))
        {
            $data = $data->toArray();
        }
        $this->assign('class',$data);
        return view();
    }
    public function table_mage(&$data = [])
    {
        $data = [
            ['field' => 'create_time', 'title' => '发帖时间'],
            ['field' => 'class_name', 'title' => '分类'],
            ['field' => 'user_name', 'title' => '发帖用户'],
            ['field' => 'content', 'title' => '帖子内容'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    public function index_handle(&$data)
    {
        $data['data'] = array_map(function($v){
            $v['class_name'] = $v['post_class']['name'];
            $v['user_name'] = $v['user_index']['nickname'];
            if(mb_strlen($v['content']) >10){
                $content = mb_substr($v['content'],0,10);
                $v['content'] = $content.'...';
            }
            return $v;
        },$data['data']);
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
    //编辑-页面
    public function upload()
    {
        $id = input('id');
        $data = $this->model->where('id', $id)->with($this->with)->find()->toArray();
        
        $this->assign('data', $data);
        return view();
    }

}
